package logic

import (
	"context"
	"errors"
	"shuqi-novel/models/mysql"
	"shuqi-novel/pkg"

	"shuqi-novel/userrpc/internal/svc"
	"shuqi-novel/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录
func (l *UserLoginLogic) UserLogin(in *userrpc.UserLoginRequest) (*userrpc.UserLoginResponse, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.User
	err := findUser.FindUserByAccount(mysql.DB, in.Account)
	if err != nil {
		return nil, err
	}
	if findUser.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	if pkg.Md5(in.Password) != findUser.Password {
		return nil, errors.New("密码不正确,请重试")
	}
	return &userrpc.UserLoginResponse{
		UserId: int64(findUser.ID),
	}, nil
}
